function JSScaleImage(clientName, id, w, h) {
  var _h= h;
  var _w = w;
  var _ctrId = id;
  var _instance = this;
  window[clientName]=clientName;
  
  this.Height = function () {
		if(arguments.length>0) _h = arguments[0];
    return _h;
  };
  
  this.Width = function () {
    if(arguments.length>0) _w = arguments[0];
    return _w;
  };
  
  this.ReloadImage = function(imgSrc, catche, onLoadComple) {
    var img = new Image();  
    img.name = _ctrId;
    if (!catche) {
			var d = new Date();
			if (imgSrc.indexOf('?') < 0) {
				imgSrc += '?dt='+d.getTime();
			} else {
				imgSrc += '&dt='+d.getTime();
			}
    }
    var instance = this;
    img.src = imgSrc+'';        
    img.onload = function () {
      var h = this.height;
      var w = this.width;
      
      var size = findSize(w, h);      
      var image = document.getElementById(_ctrId);
      image.src = this.src;
      image.height = size[1];
      image.width = size[0];    
      image.style.width = size[0]+'px';
      image.style.height = size[1]+'px';      
      if (typeof(onLoadComple) == "function") {
				onLoadComple(image, {});
      }
    }     
  };
 
  
  function findEdge (oldEdge1, newsEdge1, oldEdge2) {
    return adge = Math.round(newsEdge1 * oldEdge2 / oldEdge1);        
  };
  
  function findSize(w, h) {
    if (h >= _h && w >= _w) {
      if (_h > _w) {
        var edge = findEdge(w, _w, h);
        if (edge > _h) {
          edge = findEdge(h, _h, w);
          return [edge, _h];
        }
        
        return [_w, edge];
      } else {
        var edge = findEdge(h, _h, w);
        if (edge > _w) {
          edge = findEdge(w, _w, h);
          return [_w, edge];
        }
        
        return [edge,_h];
      }
    } else if (h >= _h) {
      var w = findEdge(h, _h, w);
      return [w, _h];
    } else if (w >= _w) {
      var h = findEdge(w, _w, h);
      return [_w, h];
    } else {
      return [w, h];
    }
  };
};